
Page({
  handleSubmit(event){
    var keyword = event.detail.value.keyword;
    wx.request({
      url: `http://192.168.4.18:3000/search?keywords=${keyword}&type=10`,
      
      header: {'content-type':'application/json'},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res)=>{
        var albums = res.data.result.albums;
        var musics = [];
        albums.forEach(item =>{
          var obj ={};
          
          if(item.name.length>8){
            item.name = item.name.slice(0,8)+"..."
          }
          if(item.artist.name.length>8){
            item.artist.name = item.artist.name.slice(0,8)+"..."
          }
          obj.name = item.name;
          obj.artistName = item.artist.name;
          obj.id = item.id;
          obj.picUrl = item.picUrl;
          musics.push(obj)
        })

        this.setData({
          musics
        })

      },
      fail: ()=>{},
      complete: ()=>{}
    });
  },
  /**
   * 页面的初始数据
   */
  data: {
    albums:"",
    prevID:"",
    playStates:{}
  },

  handleClick(event){
    let id = event.currentTarget.dataset.id;
    let playStates = this.data.playStates;

    if(id !=this.data.prevID){
      playStates[id]=true;
        for(let key in playStates){
          if(key !=id){
            playStates[key] = false;
          }
        }

        this.setData({
          playStates,
          prevID:id
        })

    }else{
      playStates[id] = !playStates[id];

      this.setData({
        playStates
      })
    }

  }

  
})